package thread;

public class ThreadDemo1 {
    public static void main(String[] args) {
        Thread t1 = new MyThread1();
        Thread t2 = new MyThread2();
        Thread t3 = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                System.out.println("What fuck!");
            }
        });
        t1.start();
        t2.start();
        t3.start();
    }
}


/**
 *  继承线程并重写run方法的形式优点在于结构简单,利于使用匿名内部类形式创建.
 *  缺点主要有两个:
 *  1:由于java是单继承的,导致如果继承了Thread就不能再继承其它类了,这在实际开发时非常不方便.
 *  2:在定义线程同时重写run方法来定义任务,这导致线程与任务存在必然的耦合关系,不利于线程的重用.
 */
class MyThread1 extends Thread {
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("What's your name?");
        }
    }
}

class MyThread2 extends Thread {
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("I'm yhy");
        }
    }
}
